home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / QuakeTools / src / libqsys / m68k / amigaos / amigaos.c next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  8.7 KB  |  288 lines

  1. #define    LIBQSYS_DRIVER
  2. #define    LIBQTOOLS_CORE
  3. #include "../../../include/libqsys.h"
  4. /*
  5.  * needed for tmalloc/.../Error
  6.  * maybe move them to this one (generic?)
  7.  */
  8. #include "../../../include/libqtools.h"
  9.  
  10. /*
  11.  * display
  12.  */
  13.  
  14. static struct DisplayDimension dim;
  15.  
  16. extern struct ExecBase *SysBase;
  17. extern struct DOSBase *DOSBase;
  18. static struct GfxBase *GfxBase;
  19. static struct IntuitionBase *IntuitionBase;
  20. static struct Library *AslBase;
  21. static struct Library *CyberGfxBase;
  22.  
  23. static int ScreenWidth = 0;
  24. static int ScreenHeight = 0;
  25. static int ScreenDepth = 0;
  26. static int ScreenLog = 0;
  27. static int ScreenLeft = 0;
  28. static int ScreenTop = 0;
  29.  
  30. static void *FrameBuffer = NULL;        /* actual framebufferaddress */
  31. static int FrameSize = 0;            /* size of updateregion in bytes */
  32. static unsigned char FrameFormat = 0;        /* default RECTFMT_LUT8   ? */
  33. static unsigned short int BytesPerRow = 0;    /* default            8/8 ? */
  34. static unsigned short int BytesPerPel = 0;    /* default            8/8 ? */
  35.  
  36. static struct Screen *Screen = NULL;
  37. static struct Window *Window = NULL;
  38. static struct ViewPort *ViewPort = NULL;
  39. static struct RastPort *RastPort = NULL;
  40.  
  41. #define SAVEBACK_COLORS (256 * 3)
  42. static unsigned short int oldNFree, oldShareable;
  43. static long unsigned int OldColorMap[1 + (256 * 3) + SAVEBACK_COLORS];
  44. static long unsigned int NewColorMap[1 + (256 * 3) + SAVEBACK_COLORS];    /* LoadRGB32-Palette */
  45.  
  46. static void *InitDisplay(void) {
  47.   /*
  48.    * Works with Windows and Screens!!!
  49.    */
  50.   if (ScreenDepth <= 8) {
  51.     BytesPerPel = 1;
  52.     FrameFormat = RECTFMT_LUT8;
  53.   }
  54.   else if (ScreenDepth <= 16) {
  55.     BytesPerPel = 2;
  56.     FrameFormat =         NULL;
  57.   }
  58.   else if (ScreenDepth <= 32) {
  59.     BytesPerPel = 4;
  60.     FrameFormat = RECTFMT_RGB;
  61.   }
  62.   BytesPerRow = ScreenWidth * BytesPerPel;
  63.   FrameSize = BytesPerRow * ScreenHeight;
  64.  
  65.   if(FrameBuffer != NULL)
  66.     tfree(FrameBuffer);
  67.   if((FrameBuffer = (void *)tmalloc(FrameSize)) == NULL)    // Bitmap allocation failed, what now??
  68.     Error("Cannot allocate FrameBuffer\n");
  69.  
  70.   return FrameBuffer;
  71. }
  72.  
  73. struct DisplayDimension *OpenDisplay(int width, int height, int depth, struct rgb *Palette) {
  74.   atexit(CloseDisplay);
  75.  
  76.   GfxBase = (struct GfxBase *)OpenLibrary((unsigned char *)"graphics.library", (unsigned long)39);
  77.   IntuitionBase = (struct IntuitionBase *)OpenLibrary((unsigned char *)"intuition.library", (unsigned long)39);
  78.   AslBase = OpenLibrary((unsigned char *)"asl.library", (unsigned long)37);
  79.   CyberGfxBase = OpenLibrary((unsigned char *)"cybergraphics.library", (unsigned long)39);
  80.  
  81.   if((GfxBase == NULL) || (IntuitionBase == NULL) || (AslBase == NULL))
  82.     Error("Cannot open one or more of the required libraries\n");
  83.  
  84.   ScreenWidth  = width;
  85.   ScreenHeight = height;
  86.   ScreenDepth  = depth;
  87.  
  88.   Screen = LockPubScreen(NULL);
  89.   if ((ScreenDepth > 8) && (GetBitMapAttr(Screen->RastPort.BitMap,BMA_DEPTH) <= 8))
  90.     Error("Screen must have more than 8bits\n");
  91.   else if ((ScreenDepth = 8) && (GetBitMapAttr(Screen->RastPort.BitMap,BMA_DEPTH) < 8))
  92.     Error("Screen must have at least 8bits\n");
  93.   else {
  94.     if ((Window = OpenWindowTags(NULL,
  95.                  WA_InnerWidth, ScreenWidth,
  96.                   WA_InnerHeight, ScreenHeight,
  97.                  WA_MinWidth, BASEWIDTH,
  98.                  WA_MinHeight, BASEHEIGHT,
  99.                  WA_MaxWidth, MAXWIDTH,
  100.                  WA_MaxHeight, MAXHEIGHT,
  101.                   WA_Left, (Screen->Width - ScreenWidth) / 2,
  102.                   WA_Top, (Screen->Height - ScreenHeight) / 2,
  103.                   WA_Flags, WFLG_GIMMEZEROZERO | WFLG_SIMPLE_REFRESH | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM | WFLG_ACTIVATE,
  104.                   WA_PubScreen, (long unsigned int)Screen,
  105.                    WA_RMBTrap, TRUE,
  106.                    WA_IDCMP, IDCMP_RAWKEY | IDCMP_NEWSIZE | IDCMP_CLOSEWINDOW | IDCMP_CHANGEWINDOW,
  107.                    WA_Title, (long unsigned int)"aQView (by Niels Fröhling)",
  108.                    WA_ScreenTitle, (long unsigned int)"QMap v1.0",
  109.                    TAG_DONE, NULL)) == NULL)    // can't open window, what now??
  110.       Error("Cannot open window\n");
  111.  
  112.     RastPort = Window->RPort;
  113.     ViewPort = &Window->WScreen->ViewPort;
  114.     GetRGB32(ViewPort->ColorMap, 0, 256, &OldColorMap[1]);
  115.     oldNFree = ViewPort->ColorMap->PalExtra->pe_NFree;
  116.     ViewPort->ColorMap->PalExtra->pe_NFree = 0;
  117.     oldShareable = ViewPort->ColorMap->PalExtra->pe_SharableColors;
  118.     ViewPort->ColorMap->PalExtra->pe_SharableColors = 0;
  119.     OldColorMap[0] = 0x01000000;
  120.   }
  121.  
  122.   ChangeDisplay(width, height, depth, Palette);
  123.  
  124.   dim.X = (int)Window->LeftEdge;
  125.   dim.Y = (int)Window->TopEdge;
  126.   dim.changedOffset = TRUE;
  127.   dim.Width = ScreenWidth = (int)Window->GZZWidth;
  128.   dim.Height = ScreenHeight = (int)Window->GZZHeight;
  129.   dim.changedSize = TRUE;
  130.   dim.dtX = (int)Window->WScreen->LeftEdge;
  131.   dim.dtY = (int)Window->WScreen->TopEdge;
  132.   dim.changedDesktopOffset = TRUE;
  133.   dim.dtWidth = (int)Window->WScreen->Width;
  134.   dim.dtHeight = (int)Window->WScreen->Height;
  135.   dim.changedDesktopSize = TRUE;
  136.   dim.frameBuffer = InitDisplay();
  137.   dim.frameSize = ScreenWidth * ScreenHeight;
  138.   dim.changedBuffer = TRUE;
  139.  
  140.   return &dim;
  141. }
  142.  
  143. void *SwapDisplay(void *oldBuffer) {
  144.   if(RastPort) {
  145.     if(CyberGfxBase) {
  146.       if(FrameFormat != 0) {
  147.         //WaitBOVP(ViewPort);
  148.         WritePixelArray(oldBuffer, 0, 0, BytesPerRow, RastPort, 0, 0, ScreenWidth, ScreenHeight, FrameFormat);
  149.       }
  150.     }
  151.     else
  152.       WriteChunkyPixels(RastPort, 0, 0, ScreenWidth, ScreenHeight, oldBuffer, BytesPerRow);
  153.   }
  154.   return oldBuffer;
  155. }
  156.  
  157. void *UpdateDisplay(void *oldBuffer, int x, int y, int width, int height) {
  158.   if(RastPort) {
  159.     if(CyberGfxBase) {
  160.       if(FrameFormat != 0) {
  161.         //WaitBOVP(ViewPort);
  162.         WritePixelArray(oldBuffer, x, y, width, RastPort, 0, 0, width, height, FrameFormat);
  163.       }
  164.     }
  165.     else
  166.       WriteChunkyPixels(RastPort, 0, 0, ScreenWidth, ScreenHeight, oldBuffer, BytesPerRow);
  167.   }
  168.   return oldBuffer;
  169. }
  170.  
  171. struct DisplayDimension *ChangeDisplay(int width, int height, int depth, struct rgb *Palette) {
  172.   if(ViewPort) {
  173.     long unsigned int *ColorMap = NewColorMap;
  174.     int c = 256-1;
  175.  
  176.     *ColorMap++ = 0x01000000;
  177.     for(; c >= 0; c--) {
  178.       *ColorMap++ = ((long unsigned int) (*((unsigned char *)Palette)++)) << 24;    /* red   */
  179.       *ColorMap++ = ((long unsigned int) (*((unsigned char *)Palette)++)) << 24;    /* green */
  180.       *ColorMap++ = ((long unsigned int) (*((unsigned char *)Palette)++)) << 24;    /* blue  */
  181.     }
  182.     LoadRGB32(ViewPort, NewColorMap);
  183.   }
  184.   return NULL;    /* not fully functional */
  185. }
  186.  
  187. void CloseDisplay(void) {
  188.   if (Window) {
  189.     LoadRGB32(ViewPort, &OldColorMap[0]);
  190.     ViewPort->ColorMap->PalExtra->pe_NFree = oldNFree;
  191.     ViewPort->ColorMap->PalExtra->pe_SharableColors = oldShareable;
  192.     CloseWindow(Window);
  193.     Window = NULL;
  194.     UnlockPubScreen(NULL, Screen);
  195.     Screen = NULL;
  196.     tfree(FrameBuffer);
  197.     FrameBuffer = NULL;
  198.   }
  199.   
  200.   if (CyberGfxBase != NULL) {
  201.     CloseLibrary(CyberGfxBase);
  202.     CyberGfxBase = NULL;
  203.   }
  204.   if (IntuitionBase != NULL) {
  205.     CloseLibrary((struct Library *)IntuitionBase);
  206.     IntuitionBase = NULL;
  207.   }
  208.   if (GfxBase != NULL) {
  209.     CloseLibrary((struct Library *)GfxBase);
  210.     GfxBase = NULL;
  211.   }
  212.   if (AslBase != NULL) {
  213.     CloseLibrary(AslBase);
  214.     AslBase = NULL;
  215.   }
  216. }
  217.  
  218. /*
  219.  * input
  220.  */
  221.  
  222. static struct MsgPort *inputPort = NULL;
  223.  
  224. void OpenKeys(void) {
  225.   if(Window)
  226.     inputPort = Window->UserPort;
  227. }
  228.  
  229. bool GetKeys(struct keyEvent *eventBuffer) {
  230.   struct IntuiMessage *intuiMsg = NULL;
  231.  
  232.   eventBuffer->pressed = RAWKEY_NOTHING;
  233.  
  234.   if(inputPort) {
  235.     Forbid();
  236.     while ((intuiMsg = (struct IntuiMessage *)GetMsg(inputPort)) != NULL) {
  237.       long unsigned int Class    = intuiMsg->Class;
  238.       unsigned short int Code    = intuiMsg->Code;
  239.       unsigned short int Qual    = intuiMsg->Qualifier;
  240.       struct Window *IDCMPWindow = intuiMsg->IDCMPWindow;
  241.  
  242.       ReplyMsg((struct Message *)intuiMsg);
  243.  
  244.       switch (Class) {
  245.         case IDCMP_RAWKEY:
  246.       eventBuffer->pressed = (unsigned char)Code;
  247.       eventBuffer->qualifier = (unsigned char)Qual;
  248.       break;
  249.         case IDCMP_CLOSEWINDOW:
  250.       eventBuffer->pressed = RAWKEY_ESCAPE;
  251.       break;
  252.         case IDCMP_NEWSIZE: {
  253.         dim.changedSize = TRUE;
  254.         dim.Width = ScreenWidth = (int)IDCMPWindow->GZZWidth;
  255.         dim.Height = ScreenHeight = (int)IDCMPWindow->GZZHeight;
  256.         dim.changedBuffer = TRUE;
  257.         dim.frameBuffer = InitDisplay();
  258.             dim.frameSize = FrameSize;
  259.  
  260.             SetDisplay(&dim);
  261.       } break;
  262.         case IDCMP_CHANGEWINDOW: {
  263.         dim.changedOffset = TRUE;
  264.             dim.X = IDCMPWindow->LeftEdge;
  265.             dim.Y = IDCMPWindow->TopEdge;
  266.  
  267.             SetDisplay(&dim);
  268.       } break;
  269.       }
  270.     }
  271.     Permit();
  272.     intuiMsg = (struct IntuiMessage *)-1;
  273.   }
  274.   
  275.   return (intuiMsg != NULL);
  276. }
  277.  
  278. void CloseKeys(void) {
  279.   if(inputPort) {
  280.     struct IntuiMessage *intuiMsg;
  281.  
  282.     Forbid();
  283.     while ((intuiMsg = (struct IntuiMessage *)GetMsg(inputPort)) != NULL)
  284.       ReplyMsg((struct Message *)intuiMsg);
  285.     Permit();
  286.   }
  287. }
  288.